﻿using System;
using System.Windows.Forms;
using Business.Forms;

namespace Business.Controls
{
    public class CommandDelete : CommandBase
    {
        public CommandDelete()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CommandDelete
            // 
            Atalho = Keys.F6;
            this.Text = "Excluir";
            this.Click += new System.EventHandler(this.CommandDelete_Click);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CommandInsert_KeyDown);
            this.ResumeLayout(false);

        }

        private void CommandInsert_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyData == Atalho)
            {
                Delete();
            }
        }

        private void Delete()
        {
            BeforeOperation();
            try
            {

                var msg = "Deseja realmente excluir este registro";
                if (MessageBox.Show(msg, "Atenção", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (Form.Delete(Form.CurrentObject))
                    {
                        Form.Form.DialogResult = DialogResult.OK;
                        Form.Form.Close();
                    }
                    else
                    {
                        Form.Form.DialogResult = DialogResult.OK;
                    }
                }

            }
            catch (Exception ex)
            {
                ExibeErro("Erro ao excluir item.\n");
            }
            AfterOperation();
        }

        private void CommandDelete_Click(object sender, System.EventArgs e)
        {
            Delete();
        }

        public override void Actived()
        {
            if (Form == null)
            {
                return;
            }
            if (Form.FormState == FormState.Inserting)
            {
                Enabled = false;
            }
            var prem = Seguranca.SegurancaUtils.GetPermissaoForm(Form.Form.Name);
            if (Form != null && Form.Permissao != null && prem.PermissaoDelete)
            {
                Enabled = true;
            }
            else
            {
                Enabled = false;
            }
        }
    }
}
